// Gruppe.cpp : Implementation of CGruppe
#include "stdafx.h"
#include "EBASdll.h"
#include "Gruppe.h"
#include "importado.h"
#include "EnumStuderende.h"
#include "EnumFag.h"

/////////////////////////////////////////////////////////////////////////////
// CGruppe

STDMETHODIMP CGruppe::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IGruppe
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CGruppe::SetInfo()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_GEM_GRUPPE";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("navn", adVarChar, adParamInput, 255, m_navn) );
		command->Parameters->Append( command->CreateParameter("startTid", adVarChar, adParamInput, 255, m_startTid) );
		command->Parameters->Append( command->CreateParameter("slutTid", adVarChar, adParamInput, 255, m_slutTid) );
		command->Parameters->Append( command->CreateParameter("sted", adVarChar, adParamInput, 255, m_sted) );
		command->Parameters->Append( command->CreateParameter("bemaerkning", adVarChar, adParamInput, 255, m_bemaerkning) );
		command->Parameters->Append( command->CreateParameter("ejerforhold", adVarChar, adParamInput, 255, m_ejerforhold) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = (bstr_t)recordset->Fields->GetItem("GUID")->Value;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CGruppe::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::get_Navn(BSTR *pVal)
{
	*pVal = m_navn.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::put_Navn(BSTR newVal)
{
	m_navn = newVal;

	return S_OK;
}

STDMETHODIMP CGruppe::get_StartTid(BSTR *pVal)
{
	*pVal = m_startTid.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::put_StartTid(BSTR newVal)
{
	m_startTid = newVal;

	return S_OK;
}

STDMETHODIMP CGruppe::get_SlutTid(BSTR *pVal)
{
	*pVal = m_slutTid.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::put_SlutTid(BSTR newVal)
{
	m_slutTid = newVal;

	return S_OK;
}

STDMETHODIMP CGruppe::get_Sted(BSTR *pVal)
{
	*pVal = m_sted.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::put_Sted(BSTR newVal)
{
	m_sted = newVal;

	return S_OK;
}

STDMETHODIMP CGruppe::get_Bemaerkning(BSTR *pVal)
{
	*pVal = m_bemaerkning.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::put_Bemaerkning(BSTR newVal)
{
	m_bemaerkning = newVal;

	return S_OK;
}

STDMETHODIMP CGruppe::get_Ejerforhold(BSTR *pVal)
{
	*pVal = m_ejerforhold.copy();

	return S_OK;
}

STDMETHODIMP CGruppe::put_Ejerforhold(BSTR newVal)
{
	m_ejerforhold = newVal;

	return S_OK;
}

STDMETHODIMP CGruppe::Slet()	
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_SLET_GRUPPE";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("guid", adVarChar, adParamInput, 255, m_guid) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = bstr_t(""); 
		m_navn = bstr_t(""); 
		m_startTid = bstr_t(""); 
		m_slutTid = bstr_t(""); 
		m_sted = bstr_t("");  
		m_bemaerkning = bstr_t(""); 
		m_ejerforhold = bstr_t(""); 
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CGruppe::TilfoejMedlem(IStuderende *newVal)
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_TILMELD_STUDERENDE_GRUPPE";

	try {
		BSTR stud;
		newVal->get_GUID(&stud);

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("gruppeGuid", adVarChar, adParamInput, 255, m_guid) );
		command->Parameters->Append( command->CreateParameter("studerendeGuid", adVarChar, adParamInput, 255, bstr_t(stud)) );

		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.Description());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CGruppe::FjernMedlem(IStuderende *newVal)
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_AFMELD_STUDERENDE_GRUPPE";

	try {
		BSTR stud;
		newVal->get_GUID(&stud);

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("gruppeGuid", adVarChar, adParamInput, 255, m_guid) );
		command->Parameters->Append( command->CreateParameter("studerendeGuid", adVarChar, adParamInput, 255, bstr_t(stud)) );

		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CGruppe::get_Medlemmer(IEnumStuderende **pVal)
{
	try {
		CComObject<CEnumStuderende> * pEnumStud;
		CComObject<CEnumStuderende>::CreateInstance(&pEnumStud);		
		pEnumStud->put_GruppeMedlemmer(m_navn);
		pEnumStud->QueryInterface(pVal);
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}


STDMETHODIMP CGruppe::TilfoejFag(IFag *newVal, BSTR startDato, BSTR slutDato)
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_TILMELD_FAG_GRUPPE";

	try {
		BSTR stud;
		newVal->get_GUID(&stud);

		bstr_t start = startDato;
		bstr_t slut = slutDato;

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("gruppeGuid", adVarChar, adParamInput, 255, m_guid) );
		command->Parameters->Append( command->CreateParameter("startDato", adVarChar, adParamInput, 255, start) );
		command->Parameters->Append( command->CreateParameter("slutDato", adVarChar, adParamInput, 255, slut) );
		command->Parameters->Append( command->CreateParameter("fagGuid", adVarChar, adParamInput, 255, bstr_t(stud)) );

		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}

STDMETHODIMP CGruppe::FjernFag(IFag *newVal)
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_AFMELD_FAG_GRUPPE";

	try {
		BSTR stud;
		newVal->get_GUID(&stud);

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("gruppeGuid", adVarChar, adParamInput, 255, m_guid) );
		command->Parameters->Append( command->CreateParameter("fagGuid", adVarChar, adParamInput, 255, bstr_t(stud)) );

		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}

	return S_OK;
}
